package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.sumpu.app.guoguojie.dao.MentionDao;
import cn.sumpu.app.guoguojie.domain.Doing;
import cn.sumpu.app.guoguojie.domain.Mention;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class MentionDaoImpl extends SqlSessionDaoSupport implements MentionDao {
	private final static Logger logger = LoggerFactory
			.getLogger(MentionDaoImpl.class);

	// 添加一条提到我的记录
	@Override
	public void addMention(Mention mention) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("cn.sumpu.app.guoguojie.persistence.MentionMapper.addMention",
							mention);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("addMention database failure.", e);
			}
			throw new AppBusinessException("addMention database failure.", e);
		}
	}

	// 查询有多少条提到我家的新记录
	@Override
	public Integer getMentionCount(String doorplate)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne(
							"cn.sumpu.app.guoguojie.persistence.MentionMapper.getMentionCount",
							doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMentionCount database failure.", e);
			}
			throw new AppBusinessException("getMentionCount database failure.", e);
		}
	}

	//更改家里的提到我家的信息为已读
	@Override
	public void clearMentionByDoorplate(String doorplate)
			throws AppBusinessException {
		try {
			getSqlSession().update("cn.sumpu.app.guoguojie.persistence.MentionMapper.clearMentionByDoorplate",
							doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("clearMentionByDoorplate database failure.", e);
			}
			throw new AppBusinessException("clearMentionByDoorplate database failure.", e);
		}
	}

	//根据门牌号取得提到我家的信息
	@SuppressWarnings("unchecked")
	@Override
	public List<Mention> getMentionByDoorplate(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MentionMapper.getMentionByDoorplate",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMentionByDoorplate database failure.", e);
			}
			throw new AppBusinessException("getMentionByDoorplate database failure.", e);
		}
	}

	//根据门牌号取得提到我家的信息
	@SuppressWarnings("unchecked")
	@Override
	public List<Mention> getMentionIdByDoorplate(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MentionMapper.getMentionIdByDoorplate",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMentionIdByDoorplate database failure.", e);
			}
			throw new AppBusinessException("getMentionIdByDoorplate database failure.", e);
		}
	}

	//根据会员ID取得家的门牌号
	@Override
	public String getDoorplateByMember(String member)
			throws AppBusinessException {
		try {
			return (String) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MentionMapper.getDoorplateByMember",
					member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDoorplateByMember database failure.", e);
			}
			throw new AppBusinessException("getDoorplateByMember database failure.", e);
		}
	}

	//根据门牌号取得提到我家的信息的总数（用于分页）
	@Override
	public Integer getMentionCountByDoorplate(String doorplate)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MentionMapper.getMentionCountByDoorplate",
					doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMentionCountByDoorplate database failure.", e);
			}
			throw new AppBusinessException("getMentionCountByDoorplate database failure.", e);
		}
	}

	//根据群组里面的话题ID取得家的门牌号
	@Override
	public String getDoorplateByTopicId(String topicId)
			throws AppBusinessException {
		try {
			return (String) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MentionMapper.getDoorplateByTopicId",
					topicId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDoorplateByTopicId database failure.", e);
			}
			throw new AppBusinessException("getDoorplateByTopicId database failure.", e);
		}
	}

	@Override
	public Doing getDoingById(String doingId) throws AppBusinessException {
		try {
			return (Doing) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MentionMapper.getDoingById",
					doingId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDoingById database failure.", e);
			}
			throw new AppBusinessException("getDoingById database failure.", e);
		}
	}

	//根据群标题的回复ID删除提到我家的
	@Override
	public void deleteMentionById(String topicReplyId)
			throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.MentionMapper.deleteMentionById",
					topicReplyId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteMentionById database failure.", e);
			}
			throw new AppBusinessException("deleteMentionById database failure.", e);
		}
	}

	//根据群标题的ID删除提到我家的
	@Override
	public void deleteMentionByCrowdTopicId(String replyId)
			throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.MentionMapper.deleteMentionByCrowdTopicId",
					replyId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteMentionByCrowdTopicId database failure.", e);
			}
			throw new AppBusinessException("deleteMentionByCrowdTopicId database failure.", e);
		}
	}
	//根据条件批量删除提到我家的
	@Override
	public void deleteMentionByCondition(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.MentionMapper.deleteMentionByCondition",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteMentionByCondition database failure.", e);
			}
			throw new AppBusinessException("deleteMentionByCondition database failure.", e);
		}
	}

	//根据条件批量屏蔽提到我家的
	@Override
	public void updateMentionByCondition(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession().update("cn.sumpu.app.guoguojie.persistence.MentionMapper.updateMentionByCondition",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateMentionByCondition database failure.", e);
			}
			throw new AppBusinessException("updateMentionByCondition database failure.", e);
		}
	}

}
